package com.itheima.web;


import com.itheima.result.R;
import com.itheima.util.AliOSSUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.UUID;

/**
 * @Author: 斌
 * @DateTime: 2023年02月07日  21:23:08
 * @Description: com.itheima.web
 * @Version: 1.0
 */
@Slf4j
@RestController
@RequestMapping("/common")
public class CommonController {

    @Autowired
    private AliOSSUtils aliOSSUtils;

    /**
     * 上传
     */


    @PostMapping("/upload")
    public R upload(MultipartFile file) throws IOException {


        //log.info("要上传的文件：{}", file.toString());
        //    获取原始文件名
        String originalFilename = file.getOriginalFilename();
        //    对原始文件进行重命名
        assert originalFilename != null;
        String suffix = originalFilename.substring(originalFilename.lastIndexOf("."));
        String fileName = UUID.randomUUID() + suffix;

       aliOSSUtils.upload(fileName,file);


        //log.info("存储路径:{}", basePath + fileName);
        //    把文件名返回给前端
        return R.success(fileName);
    }

    @GetMapping("/download")
    //这里的response是因为要回显数据
    public void download(String name, HttpServletResponse response) {
        //日志
        log.info("开始下载回显........");


            aliOSSUtils.download(name,response);


    }
}







